<?php

Yii::import('application.modules.ninio.models._base.BaseDatosAntropomorficos');

class DatosAntropomorficos extends BaseDatosAntropomorficos {

    /**
     * @return DatosAntropomorficos
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public static function label($n = 1) {
        return Yii::t('app', 'Datos antropomorficos|Datos antropomorficos', $n);
    }

    public function attributeLabels() {
        return array_merge(parent::relations(), array(
            'talla' => Yii::t('app', 'Talla (cm)'),
            'peso' => Yii::t('app', 'Peso (kg)'),
            'imc' => Yii::t('app', 'IMC'),
            'edad' => Yii::t('app', 'Edad (meses)'),
        ));
    }

    public function generarReport($inicio, $idNinio) {
        
//        var_dump($inicio);
        $ini = substr($inicio, 0, 10);
        $inicio = DateTime::createFromFormat('Y-m-d', $ini)->setTime(0, 0, 0);
        $fin = DateTime::createFromFormat('Y-m-d', $ini)->setTime(0, 0, 0);
        date_add($fin, date_interval_create_from_date_string('60 months'));
        $interval = DateInterval::createFromDateString('1 month');
        $period = new DatePeriod($inicio, $interval, $fin); //frecuencia del intervalo a calcular
//        var_dump($period);
        $report = array();
        $report['xAxis']['categories'] = array();
        $report['series'] = array();
        $data = array();
        $i=0;$j=0;
        foreach ($period as $date) {
            
            $date->modify('first day of this month');
            $inicio = $date->format('Y-m-d H:i:s');
            if($i==12){
                $j++;
                $i=0;
            }
            $categoria = ($i==0)?$j."<b>"." año(s) "."</b>":$i ;
//             var_dump($i."   ".$j);
//                    $j." año(s) ".$i." mes(es) ";
//            $categoria = $date->format('M-Y');
            $report['xAxis']['categories'][] = $categoria;
            $date->modify('last day of this month');
            $date->add(new DateInterval('PT23H59M59S'));
            $fin = $date->format('Y-m-d H:i:s');
            $cons = $this->consulta($idNinio, $inicio, $fin);
            array_push($data, $cons);
            $i++;
            
//            var_dump($inicio." ".$fin);
        }
        array_push($report['xAxis']['categories'],'5<b> año(s) </b>');
        $ninio = Ninios::model()->findByPk($idNinio);
        $pesoBajo = array();
        $pesoNormal = array();
        $pesoSuper = array();
        if ($ninio->genero == "MASCULINO") {
            $pesoBajo = array(11.3, 12.6,13.8,14.4,14.7,14.8,14.9,14.9,14.9,14.8,14.7,14.6,14.5,14.4,14.3,14.2,14.2,14.1,14.0,13.9,13.9,13.8,13.8,13.7         ,13.9       ,13.9,13.8,13.8,13.8,13.7,13.7,13.7,13.6,13.6,13.5,13.5,13.5,13.5,13.4,13.4,13.4,13.3,13.3,13.3,13.3,13.2,13.2,13.2,13.2,13.2,13.2,13.1,13.1,13.1,13.1,13.1,13.1,13.0,13.0,13.0,13.0);
//            $pesoBajo = array(11.3, 12.6,13.8,14.4,14.7,14.8,14.9,14.9,14.9,14.8,14.7,14.6,14.5,14.4,14.3,14.2,14.2,14.1,14.0,13.9,13.9,13.8,13.8,13.7         ,13.7,13.9       ,13.9,13.8,13.8,13.8,13.7,13.7,13.7,13.6,13.6,13.5,13.5,13.5,13.5,13.4,13.4,13.4,13.3,13.3,13.3,13.3,13.2,13.2,13.2,13.2,13.2,13.2,13.1,13.1,13.1,13.1,13.1,13.1,13.0,13.0,13.0,13.0);
            $pesoNormal = array(14.8, 16.4,17.8,18.5,18.7,18.9,18.9,18.9,18.8,18.7,18.6,18.4,18.3,18.1,18.0,17.9,17.8,17.6,17.5,17.4,17.4,17.3,17.2,17.1       , 17.4     ,17.4,17.3,17.3,17.2,17.2,17.2,17.1,17.1,17.0,17.0,17.0,17.0,16.9,16.9,16.9,16.8,16.8,16.8,16.8,16.8,16.8,16.7,16.7,16.7,16.7,16.7,16.7,16.7,16.7,16.7,16.7,16.7,16.7,16.7,16.7,16.7);
//            $pesoNormal = array(14.8, 16.4,17.8,18.5,18.7,18.9,18.9,18.9,18.8,18.7,18.6,18.4,18.3,18.1,18.0,17.9,17.8,17.6,17.5,17.4,17.4,17.3,17.2,17.1       ,17.1, 17.4     ,17.4,17.3,17.3,17.2,17.2,17.2,17.1,17.1,17.0,17.0,17.0,17.0,16.9,16.9,16.9,16.8,16.8,16.8,16.8,16.8,16.8,16.7,16.7,16.7,16.7,16.7,16.7,16.7,16.7,16.7,16.7,16.7,16.7,16.7,16.7,16.7);
            $pesoRiesgoSobre = array(16.1,17.6,19.2,19.8,20.1,20.2,20.3,20.3,20.2,20.1,19.9,19.8,19.6,19.5,19.3,19.2,19.1,18.9,18.8,18.7,18.6,18.6,18.5,18.4          ,18.3,          18.3,18.2,18.2,18.1,18.1,18.0,18.0,18.0,17.9,17.9,17.9,17.8,17.8,17.8,17.7,17.7,17.7,17.7,17.7,17.7,17.6,17.6,17.6,17.6,17.6,17.6,17.6,17.6,17.6,17.6,17.6,17.6,17.6,17.6,17.7,17.7);
//            $pesoRiesgoSobre = array(16.1,17.6,19.2,19.8,20.1,20.2,20.3,20.3,20.2,20.1,19.9,19.8,19.6,19.5,19.3,19.2,19.1,18.9,18.8,18.7,18.6,18.6,18.5,18.4          ,18.3,18.3,          18.3,18.2,18.2,18.1,18.1,18.0,18.0,18.0,17.9,17.9,17.9,17.8,17.8,17.8,17.7,17.7,17.7,17.7,17.7,17.7,17.6,17.6,17.6,17.6,17.6,17.6,17.6,17.6,17.6,17.6,17.6,17.6,17.6,17.6,17.7,17.7);
            $pesoSobre = array(16.9,18.3,19.9,20.6,20.9,21.0,21.1,21.1,21.0,20.8,20.7,20.5,20.4,20.2,20.1,19.9,19.8,19.7,19.6,19.5,19.4,19.3,19.2,19.1          ,19.4,         19.4,19.3,19.2,19.2,19.1,19.1,19.1,19.0,19.0,18.9,18.9,18.9,18.8,18.8,18.8,18.8,18.7,18.7,18.7,18.7,18.7,18.7,18.7,18.7,18.7,18.7,18.7,18.7,18.7,18.8,18.8,18.8,18.8,18.8,18.9,18.9);
//            $pesoSobre = array(16.9,18.3,19.9,20.6,20.9,21.0,21.1,21.1,21.0,20.8,20.7,20.5,20.4,20.2,20.1,19.9,19.8,19.7,19.6,19.5,19.4,19.3,19.2,19.1          ,19.1,19.4,         19.4,19.3,19.2,19.2,19.1,19.1,19.1,19.0,19.0,18.9,18.9,18.9,18.8,18.8,18.8,18.8,18.7,18.7,18.7,18.7,18.7,18.7,18.7,18.7,18.7,18.7,18.7,18.7,18.7,18.8,18.8,18.8,18.8,18.8,18.9,18.9);
        } else {
            $pesoNormal = array(14.7, 16.1, 17.4, 18.0, 18.3, 18.5, 18.6, 18.6, 18.5, 18.4, 18.2, 18.1, 17.9, 17.8, 17.7, 17.5, 17.4, 17.3, 17.2, 17.2, 17.1, 17.0, 17.0, 16.9, 17.2, 17.1, 17.1, 17.1, 17.0, 17.0, 17.0, 17.0, 16.9, 16.9, 16.9, 16.9, 16.9, 16.8, 16.8, 16.8, 16.8, 16.8, 16.8, 16.8, 16.8, 16.8, 16.8, 16.8, 16.8, 16.8, 16.8, 16.8, 16.9, 16.9, 16.9, 16.9, 16.9, 16.9, 16.9, 16.9, 17.0);
//            $pesoNormal =      array(14.7,16.1,17.4,18.0,18.3,18.5,18.6,18.6,18.5,18.4,18.2,18.1,17.9,17.8,17.7,17.5,17.4,17.3,17.2,17.2,17.1,17.0,17.0,16.9              ,16.9,17.2,          17.1,17.1,17.1,17.0,17.0,17.0,17.0,16.9,16.9,16.9,16.9,16.9,16.8,16.8,16.8,16.8,16.8,16.8,16.8,16.8,16.8,16.8,16.8,16.8,16.8,16.8,16.8,16.9,16.9,16.9,16.9,16.9,16.9,16.9,16.9,17.0);
            $pesoBajo = array(11.2, 12.1, 13.2, 13.7, 14.0, 14.2, 14.3, 14.3, 14.3, 14.2, 14.1, 14.0, 13.9, 13.8, 13.7, 13.7, 13.6, 13.5, 13.4, 13.4, 13.3, 13.3, 13.3, 13.2, 13.5, 13.4, 13.4, 13.4, 13.4, 13.4, 13.3, 13.3, 13.3, 13.3, 13.2, 13.2, 13.2, 13.2, 13.2, 13.1, 13.1, 13.1, 13.1, 13.0, 13.0, 13.0, 13.0, 13.0, 12.9, 12.9, 12.9, 12.9, 12.9, 12.9, 12.9, 12.9, 12.8, 12.8, 12.8, 12.8, 12.8);
//            $pesoBajo =        array(11.2,12.1,13.2,13.7,14.0,14.2,14.3,14.3,14.3,14.2,14.1,14.0,13.9,13.8,13.7,13.7,13.6,13.5,13.4,13.4,13.3,13.3,13.3,13.2                  ,13.2,13.5,         13.4,13.4,13.4,13.4,13.4,13.3,13.3,13.3,13.3,13.2,13.2,13.2,13.2,13.2,13.1,13.1,13.1,13.1, 13.0,13.0,13.0,13.0,13.0         ,12.9,12.9       ,12.9,12.9,12.9,12.9,12.9,12.9,12.8,12.8,12.8,12.8,12.8);
            $pesoRiesgoSobre = array(15.9, 17.3, 18.8, 19.4, 19.8, 20.0, 20.1, 20.1, 20.0, 19.9, 19.7, 19.6, 19.4, 19.2, 19.1, 19.0, 18.8, 18.7, 18.6, 18.5, 18.5, 18.4, 18.3, 18.3, 18.2, 18.5, 18.5, 18.5, 18.4, 18.4, 18.4, 18.3, 18.3, 18.3, 18.2, 18.2, 18.2, 18.2, 18.2, 18.2, 18.2, 18.2, 18.2, 18.2, 18.2, 18.3, 18.3, 18.3, 18.3, 18.3, 18.3, 18.4, 18.4, 18.4, 18.4, 18.4, 18.5, 18.5, 18.5, 18.5, 18.6);
//            $pesoRiesgoSobre = array(15.9,17.3,18.8,19.4,19.8,20.0,20.1,20.1,20.0,19.9,19.7,19.6,19.4,19.2,19.1,19.0,18.8,18.7,18.6,18.5,18.5,18.4,18.3,18.3                   ,18.2,18.5,          18.5,18.5,18.4,18.4,18.4,18.3,18.3,18.3,18.2,18.2,18.2,18.2,18.2,18.2,18.2,18.2,18.2,18.2,18.2,18.3,18.3,18.3,18.3,18.3,18.3,18.4,18.4,18.4,18.4,18.4,18.5,18.5,18.5,18.5,18.6);
            $pesoSobre = array(16.6, 18.0, 19.5, 20.3, 20.6, 20.8, 20.9, 20.9, 20.7, 20.6, 20.4, 20.2, 20.1, 19.9, 19.8, 19.7, 19.5, 19.4, 19.3, 19.3, 19.2, 19.1, 19.1, 19.0, 19.3, 19.3, 19.3, 19.2, 19.2, 19.2, 19.1, 19.1, 19.1, 19.0, 19.0, 19.0, 19.0, 19.0, 19.0, 19.0, 19.0, 19.0, 19.0, 19.1, 19.1, 19.1, 19.1, 19.1, 19.2, 19.2, 19.2, 19.2, 19.3, 19.3, 19.3, 19.4, 19.4, 19.4, 19.4, 19.5, 19.5);
//            $pesoSobre = array(16.6,18.0,19.5,20.3,20.6,20.8,20.9,20.9,20.7,20.6,20.4,20.2,20.1,19.9,19.8,19.7,19.5,19.4,19.3,19.3,19.2,19.1,19.1                              ,19.0,19.3,         19.3,19.3,19.2,19.2,19.2,19.1,19.1,19.1,19.0,19.0,19.0,19.0,19.0,19.0,19.0,19.0,19.0,19.0,19.1,19.1,19.1,19.1,19.1,19.2,19.2,19.2,19.2,19.3,19.3,19.3,19.4,19.4,19.4,19.4,19.5,19.5);
        }
//        var_dump(count($pesoNormal) . " " . count($pesoBajo) . " " . count($pesoRiesgoSobre) . " " . count($pesoSobre));
//        var_dump($pesoBajo);
        array_push($report['series'], array('name' => "Sobrepeso ", 'data' => $pesoSobre));
        array_push($report['series'], array('name' => "Riesgo sobrepeso ", 'data' => $pesoRiesgoSobre));
        array_push($report['series'], array('name' => "Peso normal", 'data' => $pesoNormal));
        array_push($report['series'], array('name' => "Bajo peso", 'data' => $pesoBajo));
//        array_push($report['series'], array('name' => "p99 ", 'data' => $pesoSobre,'type'=>'spline','colors'=>'#000000'));
//        array_push($report['series'], array('name' => "p97 ", 'data' => $pesoRiesgoSobre,'type'=>'spline','colors'=>'#000000'));
//        array_push($report['series'], array('name' => "p85", 'data' => $pesoNormal,'type'=>'spline','colors'=>'#000000'));
//        array_push($report['series'], array('name' => "p3", 'data' => $pesoBajo,'type'=>'spline','colors'=>'#000000'));
        array_push($report['series'], array('name' => "Bmi de " . Ninios::model()->findByPk($idNinio), 'data' => $data, 'type' => 'spline'));
//       die();
        return $report;
    }

    public function consulta($idninio, $inicio, $fin) {
        $data = array();
//        SELECT datos_antropomorficos.edad as edadMeses,datos_antropomorficos.imc as IMC  FROM `datos_antropomorficos` 
//        left join `ninios` on datos_antropomorficos.ninios_id=ninios.id where ninios.id=19  order  by datos_antropomorficos.fecha desc
//        var_dump($inicio." ".$fin);
        $command = Yii::app()->db->createCommand()
                ->select('da.imc  as imc')
                ->from('datos_antropomorficos da')
                ->leftJoin("ninios n", "da.ninios_id=n.id")
                ->where("n.id=:idNinio", array(":idNinio" => $idninio))
                ->andWhere("da.fecha between :inicio and :fin", array(":inicio" => $inicio, ":fin" => $fin))
                ->order("da.fecha DESC");
        $options = $command->queryAll();
        if ($options != null) {
            $data[] = (float) $options[0]['imc'];
        } else {
            return null;
        }
        return $data;
    }

    public function reportarIngresoImcMensual($idninio) {
        $ini = Util::FechaActual();
        $ini = substr($ini, 0, 10);
        $date = DateTime::createFromFormat('Y-m-d', $ini)->setTime(0, 0, 0);
        $date->modify('first day of this month');
        $inicio = $date->format('Y-m-d H:i:s');
        $date->modify('last day of this month');
        $date->add(new DateInterval('PT23H59M59S'));
        $fin = $date->format('Y-m-d H:i:s');
        $dato = $this->consulta($idninio, $inicio, $fin);

        return $dato;
    }

}
